/**
 * @file gtkAjout.c
 * @brief Fichier source du module gtkAjout.
 *
 * @author Rachid Delory, Mamadou Keita, Florian Bernard
 */

/* *********************************************************************

                       Librairies de gtkAjout

********************************************************************* */

#include "gtkAjout.h"

/* *********************************************************************

                        Fonctions                                      

********************************************************************* */
void reponseDialogueCB(GtkDialog *dialog,
		       gint       response_id,
		       gpointer   user_data)
{
  GList *list = user_data;
  AjoutArbre *aArbre;
  aArbre = g_list_nth_data(list, 0);
  if(response_id == GTK_RESPONSE_ACCEPT)
    {
      insertion_DReg(getArbre_GAArb(aArbre), NULL);
    }
  gtk_tree_store_clear(aArbre->gtkArbre->store);
  detruire_Arb(&(aArbre->gtkArbre->arbre));
  aArbre->gtkArbre->arbre = creer_Arb(NULL);
  recuperation_DReg(aArbre->gtkArbre->arbre, 0, 1);
  recuperation_DOeu(aArbre->gtkArbre->arbre, 0);
  recuperation_Arbre_GArb(aArbre->gtkArbre->store, aArbre->gtkArbre->arbre, NULL, 1);

  detruire_GAArb(&aArbre);
  g_list_free(list);
  gtk_widget_destroy(GTK_WIDGET(dialog));
}

void creer_GAjo(GtkWidget *parent, GtkPrincipal *principal)
{
  GtkWidget *widget, *contentArea;
  GtkWidget *vbox;
  GtkWidget *panel;
  AjoutPane *aPane;
  AjoutArbre *aArbre;
  GList *list = NULL;
  
  widget = gtk_dialog_new_with_buttons("Création",
				       GTK_WINDOW(parent),
				       GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
				       GTK_STOCK_SAVE,
				       GTK_RESPONSE_ACCEPT,
				       GTK_STOCK_CANCEL,
				       GTK_RESPONSE_REJECT,
				       NULL);
  gtk_window_set_default_size(GTK_WINDOW(widget), 800, 600);
  contentArea = gtk_dialog_get_content_area (GTK_DIALOG (widget));
  vbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_container_add(GTK_CONTAINER(contentArea), vbox);

  gtk_box_set_child_packing(GTK_BOX(contentArea), vbox, TRUE, TRUE, 1, GTK_PACK_START);

  panel = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
  gtk_box_pack_start(GTK_BOX(vbox), panel, TRUE, TRUE, 1);
  aPane = creer_GAPan();
  aArbre = creer_GAArb(aPane, principal->gArbre);
  ajoutListenerTitre_GAPan(aPane, aArbre, titreChanged);
  ajoutListenerType_GAPan(aPane, aArbre, typeChanged_AArb);
  gtk_paned_add1(GTK_PANED(panel), getContainer_GAPan(aPane));
  gtk_paned_add2(GTK_PANED(panel), getContainer_GAArb(aArbre));
  
  gtk_paned_set_position(GTK_PANED(panel), 400);

  gtk_widget_show_all(widget);
  gtk_widget_set_visible(getContainer_GAPan(aPane), FALSE);
  list = g_list_append(list, aArbre);
  list = g_list_append(list, principal);
  g_signal_connect(widget, "response", G_CALLBACK(reponseDialogueCB), list); 
}
